﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using TjZheng.Framework;
using TjZheng.Controls;
using TjZheng.Domain.Business;
using TjZheng.Domain.DataEntity;

namespace TjZheng.UpLoad
{
    public static partial class UpLoadifyExtension
    {
        /// <summary>
        /// 多图片上传控件 jquery.uploadify.js
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="objUpLoadSet">上传配置</param>
        /// <returns></returns>
        public static MvcHtmlString UpLoadImages(this HtmlHelper htmlHelper, UpLoadSet objUpLoadSet)
        {
            objUpLoadSet.IsMulti = true;
            objUpLoadSet.UpLoadSuccess = "MultiImgsSuccess";
            BizUploadLimit objBizUploadLimit = new BizUploadLimit();
            UploadLimit objUploadLimit = null;
            if (objUpLoadSet.Name.IsNull())
            {
                throw new Exception("提交到服务器的Name不能为空");
            }
            if (objUpLoadSet.ULId > 0)
            {
                objUploadLimit = objBizUploadLimit.GetRecord(objUpLoadSet.ULId);
            }
            else
            {
                if (objUpLoadSet.CategoryCode.IsNoNull() && objUpLoadSet.LimitCode.IsNoNull())
                {
                    objUploadLimit = objBizUploadLimit.GetRecord(string.Format("UcCode='{0}' AND LimitCode='{1}'", objUpLoadSet.CategoryCode, objUpLoadSet.LimitCode));
                }
            }
            if (objUploadLimit == null)
            {
                throw new Exception("未设置上传文件配置");
            }
            if (objUploadLimit.UcCode.IsNull() || objUploadLimit.LimitCode.IsNull())
            {
                throw new Exception("上传文件的资源码和限制码不能为空");
            }
            string selector = objUpLoadSet.Name.Replace(".", "_");
            StringBuilder objStringBuilder = new StringBuilder();
            objStringBuilder.AppendLine(string.Format("<input id=\"{0}\" data-options='{{\"ULId\":\"{1}\",\"CategoryCode\":\"{2}\",\"LimitCode\":\"{3}\",Name:\"{4}\",\"ServerPath\":\"{5}\"}}' type=\"file\" />", selector, objUploadLimit.Id, objUploadLimit.UcCode, objUploadLimit.LimitCode, objUpLoadSet.Name, objUploadLimit.ServerPath));
            objStringBuilder.AppendLine(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\" />", objUpLoadSet.Name, objUpLoadSet.Value));
            objStringBuilder.AppendLine("<script type=\"text/javascript\">");
            string tempName = string.Format("objFileUpload_{0}", Guid.NewGuid().ToString().Replace("-", ""));
            objStringBuilder.AppendLine(string.Format("var {0} = new FileUpload(\"{1}\");", tempName, selector.FormatSelf("#{0}")));
            if (objUpLoadSet.IsMulti)
            {
                objStringBuilder.AppendLine(string.Format("{0}.multi = true;", tempName));
            }
            objStringBuilder.AppendLine(string.Format("{0}.url = \"{1}\";", tempName, objUpLoadSet.Url));
            objStringBuilder.AppendLine(string.Format("{0}.formData = {{ \"UpLoadSet\": $('{1}').attr(\"data-options\") }};", tempName, selector.FormatSelf("#{0}")));
            objStringBuilder.AppendLine(string.Format("{0}.fileObjName = \"{1}\";", tempName, objUpLoadSet.FileName));
            objStringBuilder.AppendLine(string.Format("{0}.fileTypeExts = '{1}';", tempName, objUploadLimit.LimitExtension));
            if (objUploadLimit.LimitSize > 0)
            {
                objStringBuilder.AppendLine(string.Format("{0}.fileSizeLimit = {1};", tempName, objUploadLimit.LimitSize));
            }
            objStringBuilder.AppendLine(string.Format("{0}.uploadSuccess = {1};", tempName, objUpLoadSet.UpLoadSuccess));
            if (!string.IsNullOrEmpty(objUpLoadSet.ItemTemplate))
            {
                objStringBuilder.AppendLine(string.Format("{0}.itemTemplate = \"{1}\";", tempName, objUpLoadSet.ItemTemplate));
            }
            //延迟加载，防止谷歌崩溃
            objStringBuilder.AppendLine(string.Format("$(function () {{ setTimeout(function () {{{0}.uploadify(); }}, 10);}});", tempName));
            objStringBuilder.AppendLine("</script>");
            if (objUpLoadSet.Value.IsNoNull())
            {
                objStringBuilder.AppendLine("<div class=\"upload_preview\">");
                foreach (var objImagePath in objUpLoadSet.Value.Split('$'))
                {
                    objStringBuilder.AppendLine("<div class=\"upload_append_list\" onmouseover=\"ImgOver(this);\" onmouseout=\"ImgOut(this);\">");
                    objStringBuilder.AppendLine("   <div class=\"file_bar\">");
                    objStringBuilder.AppendLine("       <div>");
                    objStringBuilder.AppendLine("           <span class=\"file_del\" data-index=\"1\" title=\"删除\" onclick=\"ImgRemove(this);\"></span>");
                    objStringBuilder.AppendLine("       </div>");
                    objStringBuilder.AppendLine("   </div>");
                    objStringBuilder.AppendLine("   <a class=\"imgBox\">");
                    objStringBuilder.AppendLine("       <div class=\"uploadImg\">");
                    objStringBuilder.AppendLine("           <img title=\"" + objImagePath.RenderImageTitle() + "\" class=\"easyui-tooltip upload_image\" data-options=\"deltaY:-20\" src=\"" + objImagePath.RenderImageSrc() + "\" onclick=\"OpenImagePreview(this);\">");
                    objStringBuilder.AppendLine("       </div>");
                    objStringBuilder.AppendLine("   </a>");
                    objStringBuilder.AppendLine("</div>");
                }
                objStringBuilder.AppendLine("<div/>");
            }
            else
            {
                objStringBuilder.AppendLine("<div class=\"upload_preview upload_no_preview\"></div>");
            }
            return MvcHtmlString.Create(objStringBuilder.ToString());
        }
        /// <summary>
        /// 多图片上传控件 jquery.uploadify.js
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="ulid">限制码Id</param>
        /// <param name="name">隐藏的Name属性</param>
        /// <param name="value">隐藏的Value值</param>
        /// <returns></returns>
        public static MvcHtmlString UpLoadImages(this HtmlHelper htmlHelper, long ulid, string name, string value)
        {
            return htmlHelper.UpLoadImages(new UpLoadSet { ULId = ulid, Name = name, Value = value });
        }
        /// <summary>
        /// 多图片上传控件 jquery.uploadify.js
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="ulid">限制码Id</param>
        /// <param name="name">隐藏的Name属性</param>
        /// <param name="value">隐藏的Value值</param>
        /// <param name="fileName">传回服务端的Name值</param>
        /// <returns></returns>
        public static MvcHtmlString UpLoadImages(this HtmlHelper htmlHelper, long ulid, string name, string value, string fileName)
        {
            return htmlHelper.UpLoadImages(new UpLoadSet { ULId = ulid, Name = name, Value = value, FileName = fileName });
        }
        /// <summary>
        /// 多图片上传控件 jquery.uploadify.js
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="ulid">限制码Id</param>
        /// <param name="name">隐藏的Name属性</param>
        /// <param name="value">隐藏的Value值</param>
        /// <param name="fileName">传回服务端的Name值</param>
        /// <param name="url">上传地址</param>
        /// <returns></returns>
        public static MvcHtmlString UpLoadImages(this HtmlHelper htmlHelper, long ulid, string name, string value, string fileName, string url)
        {
            return htmlHelper.UpLoadImages(new UpLoadSet { ULId = ulid, Name = name, Value = value, FileName = fileName, Url = url });
        }
        /// <summary>
        /// 多图片上传控件 jquery.uploadify.js
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="categoryCode">资源码</param>
        /// <param name="limitCode">限制码</param>
        /// <param name="name">隐藏的Name属性</param>
        /// <param name="value">隐藏的Value值</param>
        /// <returns></returns>
        public static MvcHtmlString UpLoadImages(this HtmlHelper htmlHelper, string categoryCode, string limitCode, string name, string value)
        {
            return htmlHelper.UpLoadImages(new UpLoadSet { CategoryCode = categoryCode, LimitCode = limitCode, Name = name, Value = value });
        }
        /// <summary>
        /// 多图片上传控件 jquery.uploadify.js
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="categoryCode">资源码</param>
        /// <param name="limitCode">限制码</param>
        /// <param name="name">隐藏的Name属性</param>
        /// <param name="value">隐藏的Value值</param>
        /// <param name="fileName">传回服务端的Name值</param>
        /// <returns></returns>
        public static MvcHtmlString UpLoadImages(this HtmlHelper htmlHelper, string categoryCode, string limitCode, string name, string value, string fileName)
        {
            return htmlHelper.UpLoadImages(new UpLoadSet { CategoryCode = categoryCode, LimitCode = limitCode, Name = name, Value = value, FileName = fileName });
        }
        /// <summary>
        /// 多图片上传控件 jquery.uploadify.js
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="categoryCode">资源码</param>
        /// <param name="limitCode">限制码</param>
        /// <param name="name">隐藏的Name属性</param>
        /// <param name="value">隐藏的Value值</param>
        /// <param name="fileName">传回服务端的Name值</param>
        /// <param name="url">上传地址</param>
        /// <returns></returns>
        public static MvcHtmlString UpLoadImages(this HtmlHelper htmlHelper, string categoryCode, string limitCode, string name, string value, string fileName, string url)
        {
            return htmlHelper.UpLoadImages(new UpLoadSet { CategoryCode = categoryCode, LimitCode = limitCode, Name = name, Value = value, FileName = fileName, Url = url });
        }
    }
}